{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1abcb063-0b52-4183-995f-50a1139f1c4c",
   "metadata": {},
   "source": [
    "<center><img src=\"./image/CLBLOGO.jpg\" alt=\"创乐博\" style=\"width: 300px;\"/></center>\n",
    "\n",
    "# 30.BMP280气压传感器实验\n",
    "\n",
    "@－－－－湖南创乐博智能科技有限公司－－－－<br>\n",
    "@  文件名：30.BMP280气压传感器实验.py <br>\n",
    "@  版本：V2.0 <br>\n",
    "@  author: zhulin<br>\n",
    "@  说明：BMP280气压传感器实验\n",
    "BMP280气压传感器实验，通过对应的adafruit_bmp280库函数，进行I2C控制方式，控制_bmp280气压传感器显示出当前气压和温度值，并在界面上打印出来！！！！！！<br>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "445be920-d473-45b8-8aa6-e4b55af36b2e",
   "metadata": {},
   "source": [
    "## 1.导入必要的库文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ab179391-9c46-48fd-92a4-d95e587c591a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "import board\n",
    "import adafruit_bmp280 # 载入bmp280 传感器库"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bbee25d9-eeff-4358-8858-030d6724a8f1",
   "metadata": {},
   "source": [
    "## 2.初始化工作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8061dfaa-9d83-4472-90c2-1fc60451907a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建传感器对象，通过主板默认的I2C总线进行通信\n",
    "i2c = board.I2C()  \n",
    "makerobo_bmp280 = adafruit_bmp280.Adafruit_BMP280_I2C(i2c,address=0x76)\n",
    "\n",
    "# 将其更改为与该位置在海平面上的压力(hPa)相匹配\n",
    "makerobo_bmp280.sea_level_pressure = 1013.25"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e02fbeb2-59db-4f0c-bf5c-78f4e5bd8804",
   "metadata": {},
   "source": [
    "## 3.循环函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e77514af-5ab2-4e49-a633-90fd484dcca2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 循环函数\n",
    "def makerobo_loop():\n",
    "    try:\n",
    "        while True:\n",
    "            print(\"\\nTemperature: %0.1f C\" % makerobo_bmp280.temperature)\n",
    "            print(\"Pressure: %0.1f hPa\" % makerobo_bmp280.pressure)\n",
    "            print(\"Altitude = %0.2f meters\" % makerobo_bmp280.altitude)\n",
    "            time.sleep(2)\n",
    "    except KeyboardInterrupt:\n",
    "        print(\"Exit\")  # Exit on CTRL+C"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "39cd4d39-7284-48c4-920c-59969df8900a",
   "metadata": {},
   "source": [
    "## 4.程序入口"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a89b2dfd-31a9-4982-9059-42b33115b115",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 程序入口\n",
    "if __name__ == \"__main__\":\n",
    "\ttry:\n",
    "\t\tmakerobo_loop()       # 循环显示信息\n",
    "\texcept KeyboardInterrupt: # 当按下Ctrl+C时，将执行destroy()子程序。\n",
    "\t\tprint(\"Exit\")  # Exit on CTRL+C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "df1088de-aa77-4a72-91ab-b8017b14b7bd",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
